<?php
try {
    if(!$_GET['xh'])throw new Exception('应该提供修改记录的学号~' );
    $db=new PDO("mysql:host=localhost; dbname=db2;", 'root', '12qwas');
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

    if ($r=$_POST){
        echo '<pre>',print_r($_POST,true),print_r($_GET,true),"</pre>";
        $ps=$db->prepare('update students set xh=?, name=?, tel=? where xh=?');
        $ps->execute([$r['xh'],$r['name'],$r['tel'],$_GET['xh']]);
        header('Location:index.php');
        return;
    }else{
        $ps=$db->prepare( 'select * from students where xh=?' );
        $ps->execute(array($_GET['xh']));
        $r=$ps->fetch();
        if($r===false)throw new Exception('找不到要修改的记录');
        echo '<pre>',print_r($r,true),"</pre>";
    }
}catch (Throwable $e){
    $msg=$e->getMessage();
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改新用户</title>
    <style>
        h1{color: red;}
        input{padding: 10px;margin: 6px;border-radius:6px; }
        .msg{color: red; margin: 20px 0;}
    </style>
</head>
<body>
<h1>修改新用户</h1>
<form method="post">
    学号：<input type="text" name="xh" value="<?=$r['xh']?>"><br>
    姓名：<input type="text" name="name" value="<?=$r['name']?>"><br>
    电话：<input type="text" name="tel" value="<?=$r['tel']?>"><br>
    <div class="msg"><?=$msg?></div>
    <input type="submit"value="提交数据">
</form>
</body>
</html>